home *** CD-ROM | disk | FTP | other *** search
- /*
- File: BindingUtils.cpp
-
- Contains: xxx put contents here xxx
-
- Owned by: Ted Jucevic
-
- Copyright: © 1996 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <4> 11/21/96 RA Added ODSetPreferredKindProps
- <3> 10/10/96 RA Moved ChangeKindAndEditor, ChangeEditor,
- IsNoPart and GetCurrentEditorForPart from
- BindngH.cpp
- <2> 10/7/96 TJ Included TempObj.h
-
- To Do:
- */
-
- /*
- File: BindingUtils.cpp
-
- Contains: Binding utilities
-
- Owned by: Reggie Adkins
-
- Copyright: © 1996 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #ifndef SOM_ODBinding_xh
- #include <ODBindng.xh>
- #endif
-
- #ifndef _TEMPOBJ_
- #include "TempObj.h"
- #endif
-
- #ifndef SOM_Module_OpenDoc_StdTypes_defined
- #include <StdTypes.xh>
- #endif
-
- #ifndef _STDTYPIO_
- #include "StdTypIO.h"
- #endif
-
- #ifndef _ISOSTR_
- #include "ISOStr.h"
- #endif
-
- #ifndef _BNDNSUTL_
- #include "BndNSUtl.h"
- #endif
-
- #ifndef _BINDINGUTILS_
- #include "BindingUtils.h"
- #endif
-
- #ifndef _INFOUTIL_
- #include "InfoUtil.h"
- #endif
-
- #ifndef _STORUTIL_
- #include "StorUtil.h"
- #endif
-
- #ifndef SOM_ODPartWrapper_xh
- #include <PartWrap.xh>
- #endif
-
- #undef LOGGING
- #define LOGGING 0
-
- //------------------------------------------------------------------------------
- // IsNoPart
- //------------------------------------------------------------------------------
-
- ODBoolean IsNoPart(Environment* ev, ODPart* part)
- {
- WASSERT(part);
-
- ODBoolean result = kODFalse;
-
- if (part)
- {
- TempODEditor editor = ((ODPartWrapper*)part)->GetEditor(ev);
-
- ODUShort length = (ODUShort)ODISOStrLength(editor);
- result = ODISOStrNCompare(editor, kODBlackBoxHandlerOfLastResort, length) == 0;
- }
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // IsNoPart
- //------------------------------------------------------------------------------
-
- ODBoolean IsNoPart(Environment* ev, ODEditor editor)
- {
- ODUShort length = (ODUShort)ODISOStrLength(editor);
-
- return (ODISOStrNCompare(editor, kODBlackBoxHandlerOfLastResort, length) == 0);
- }
-
- //------------------------------------------------------------------------------
- // IsViewerOfLastResort
- //------------------------------------------------------------------------------
-
- ODBoolean IsViewerOfLastResort(Environment* ev, ODPart* part)
- {
- WASSERT(part);
-
- ODBoolean result = kODFalse;
-
- if (part)
- {
- TempODEditor editor = ((ODPartWrapper*)part)->GetEditor(ev);
-
- result = IsViewerOfLastResort(ev,editor);
- }
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // IsViewerOfLastResort
- //------------------------------------------------------------------------------
-
- ODBoolean IsViewerOfLastResort(Environment* ev, ODEditor editor)
- {
- WASSERT(editor);
-
- ODBoolean result = kODFalse;
-
- if (editor)
- result = GetgBinding()->EditorSupportsKind(ev,editor,kODViewerOfLastResortKind);
-
- return result;
- }
-
- //------------------------------------------------------------------------------
- // ODSetPreferredEditorProps
- //------------------------------------------------------------------------------
-
- void ODSetPreferredEditorProps(ODStorageUnit* thePartSU, ODEditor theChosenEditor )
- {
- Environment* ev = somGetGlobalEnvironment();
-
- WASSERTM(thePartSU, "Not setting prefered editor props");
- WASSERTM(theChosenEditor, "Not setting prefered editor props");
-
- if(thePartSU && theChosenEditor)
- {
- // If draft is read-only we cannot write anything to the storage unit.
- ODBoolean readOnlyStorage = ( ODGetDraft(ev, thePartSU)->GetPermissions(ev) < kODDPSharedWrite );
- ODBoolean viewerOfLastResort = IsViewerOfLastResort(ev,theChosenEditor);
-
- if (!readOnlyStorage && !viewerOfLastResort)
- {
- LOG("Setting kODPropPreferredEditor to %s", theChosenEditor);
-
- if (!thePartSU->Exists(ev, kODPropPreferredEditorUserString,kODMacIText,0))
- {
- ODSUAddPropValue(ev, thePartSU, kODPropPreferredEditorUserString, kODMacIText);
-
- LOG("Created kODPropPreferredEditorUserString");
- }
-
- ODSetISOStrProp(ev, thePartSU, kODPropPreferredEditor,
- kODEditor, theChosenEditor);
- ODIText name = {0,0,0};
-
- TRY
-
- if (GetgBinding()->GetUserStringForEditor( ev, theChosenEditor, &name))
- {
- // LOG("Setting kODPropPreferredEditorUserString to %s",
- // ((ODTradITextData*)name.text._buffer)->theText); //not null terminated!
-
- ODSetITextProp(ev, thePartSU, kODPropPreferredEditorUserString,
- kODMacIText, &name);
- }
- else
- WARN("Could not find user string for %s",theChosenEditor);
-
- CATCH_ALL
- DisposeITextStruct(name);
- ENDTRY
-
- DisposeITextStruct(name);
-
- }
- else
- LOG("Not setting prefered editor props");
- }
- }
-
- //------------------------------------------------------------------------------
- // ODSetPreferredKindProps
- //------------------------------------------------------------------------------
-
- void ODSetPreferredKindProps(ODStorageUnit* thePartSU, ODType preferredkind)
- {
- Environment* ev = somGetGlobalEnvironment();
-
- WASSERTM(thePartSU, "Not setting prefered kind props");
- WASSERTM(preferredkind, "Not setting prefered kind props");
-
- if(thePartSU && preferredkind)
- {
- // If draft is read-only we cannot write anything to the storage unit.
- ODBoolean readOnlyStorage = ( ODGetDraft(ev, thePartSU)->GetPermissions(ev) < kODDPSharedWrite );
-
- if (!readOnlyStorage)
- {
- LOG("Setting kODPropPreferredKind to %s", preferredkind);
-
- if (!thePartSU->Exists(ev, kODPropPreferredKindUserString,kODMacIText,0))
- {
- ODSUAddPropValue(ev, thePartSU, kODPropPreferredKindUserString, kODMacIText);
-
- LOG("Created kODPropPreferredKindUserString");
- }
-
- ODSetISOStrProp(ev, thePartSU, kODPropPreferredKind,
- kODISOStr, preferredkind);
- ODIText name = {0,0,0};
-
- TRY
-
- if (GetgBinding()->GetUserStringForKind( ev, preferredkind, &name))
- {
- // LOG("Setting kODPropPreferredKindUserString to %s",
- // ((ODTradITextData*)name.text._buffer)->theText); //not null terminated!
-
- ODSetITextProp(ev, thePartSU, kODPropPreferredKindUserString,
- kODMacIText, &name);
- }
- else
- WARN("Could not find user string for %s",preferredkind);
-
- CATCH_ALL
- DisposeITextStruct(name);
- ENDTRY
-
- DisposeITextStruct(name);
-
- }
- else
- LOG("Not setting prefered kind props");
- }
- }
-
- //------------------------------------------------------------------------------
- // GetCurrentEditorForPart
- //------------------------------------------------------------------------------
- ODEditor GetCurrentEditorForPart(ODPart* part )
- {
- WASSERT(part);
-
- ODEditor editor = kODNULL;
-
- if (part)
- {
- Environment* ev = somGetGlobalEnvironment();
-
- editor = ((ODPartWrapper*)part)->GetEditor(ev);
- }
-
- return editor;
- }
-
- //------------------------------------------------------------------------------
- // ChangeEditor
- //------------------------------------------------------------------------------
-
- void ChangeEditor(ODPart* part, ODEditor editorID)
- {
- Environment* ev = somGetGlobalEnvironment();
- ((ODPartWrapper*)part)->UseEditor(ev, editorID);
- ODStorageUnit* thePartSU = part->GetStorageUnit(ev);
- ODSetISOStrProp(ev, thePartSU, kODPropPreferredEditor, kODEditor, editorID);
- }
-
- //------------------------------------------------------------------------------
- // ChangeKindAndEditor
- //------------------------------------------------------------------------------
-
- void ChangeKindAndEditor (Environment *ev,
- ODPart* part,
- ODType newKind,
- ODEditor newEditor,
- ODNameSpaceManager* nsm)
- {
- ODStorageUnit* partSU = part->GetStorageUnit(ev); ODVolatile(partSU);
- TempODEditor curEditor = GetCurrentEditorForPart(part);
- TempODType curKind = ODGetKindFromPartSU(ev, partSU);
-
- if ( ODISOStrEqual(curEditor, newEditor) )
- {
- if ( !ODISOStrEqual(curKind, newKind) )
- part->ChangeKind(ev, newKind);
- }
- else if ( partSU->Exists(ev, kODPropContents, newKind, 0) )
- {
- // Before changing the editor, ensure newKind is set as
- // the preferred property
- ODULong unused;
- TempODType oldKind = ODGetISOStrProp(ev, partSU, kODPropPreferredKind, kODISOStr, kODNULL, &unused);
- ODSetISOStrProp(ev, partSU, kODPropPreferredKind, kODISOStr, newKind);
- TRY
- ChangeEditor(part, newEditor);
- CATCH_ALL
- // If the editor didn't change, restore the preferred kind
- TempODEditor failEditor = GetCurrentEditorForPart(part);
- if ( ODISOStrEqual(failEditor, curEditor) )
- {
- if ( (ODType) oldKind != kODNULL )
- {
- ODSetISOStrProp(ev, partSU, kODPropPreferredKind, kODISOStr, oldKind);
- }
- else
- {
- if ( ODSUExistsThenFocus(ev, partSU, kODPropPreferredKind, kODNULL) )
- partSU->Remove(ev);
- }
- }
- RERAISE;
- ENDTRY
- }
- else if ( EditorSupportsKind(nsm, curEditor, newKind) )
- {
- // change kind first, then editor
- part->ChangeKind(ev, newKind);
-
- // verify that the change took place before changing editor
- TempODType tempKind = ODGetKindFromPartSU(ev,partSU);
- if ( ODISOStrEqual(tempKind, newKind) )
- ChangeEditor(part, newEditor);
- }
- else if ( EditorSupportsKind(nsm, newEditor, curKind) )
- {
- // change editor first, then kind
- ChangeEditor( part, newEditor);
- part->ChangeKind(ev, newKind);
-
- // if the kind didn't actually change, revert back to curEditor
- TempODType tempKind = ODGetKindFromPartSU(ev,partSU);
- if ( ODISOStrEqual(tempKind, curKind) )
- ChangeEditor(part, curEditor);
- }
- else
- {
- // Internal error; editors do not support a common kind and
- // translation has not been performed.
- WARN("ChangeKindAndEditor cannot change kind or editor");
- }
- }